home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / AI88PATT / PATHNAME.C < prev    next >
C/C++ Source or Header  |  1990-01-06  |  5KB  |  223 lines

  1. /* The functions getfile() and putfile use a Standard File GetFile or
  2. PutFile Dialog Box to retrieve the full path of a TEXT file which can
  3. be used a the file name argument to fopen.
  4.  
  5. Both getfile(char* filename) and 
  6. putfile(char* filename,char* prompt,char* defaultfile) return
  7. the full path of the file in the argument 'filename'.
  8. The int return value is the number of characters in the name.  Zero is
  9. returned if the file name couldn't be fetched and -1 is returned if
  10. the path name has more than 255 chars.  `prompt' and `defaultfile' are
  11. a titling prompt string and a filename to be highlighted in the
  12. putfile() dialog box.
  13.  
  14. Written by Jeff Meredith 12/88. Thanks to Mike Prange for his encouragement
  15. to add functionality to getfile() and for the MacTutor Loan. 
  16.  
  17. Getfile(), Putfile(), and Pathname() were streamlined by Prange, dec. 1988.
  18.  
  19. Usage:  [link with MacTraps]
  20. ***************************************************/
  21. #include <stdio.h>
  22.  
  23. /*main()
  24. {
  25.     char name1[255],name2[255];
  26.     FILE *fp1,*fopen();
  27.     
  28.     if(getfile(name1) != 0)    
  29.         fp1 = fopen(name1,"r");*/
  30. /*    ...  */
  31. /*    fclose(fp1);
  32.  
  33.     if(putfile(name2,"Name of output file:","Output.out") != 0);
  34.         fp1 = fopen(name2,"w");*/
  35. /*    ...  */
  36.  
  37. /*    fclose(fp1);
  38.     
  39.     printf("%s\n%s\n",name1,name2);
  40. }*/
  41. /***************************************************
  42. */
  43.  
  44. #include <stdio.h>
  45. #include <HFS.h>
  46. #include <WindowMgr.h>
  47. #include <StdFilePkg.h>
  48. #include <pascal.h>
  49.  
  50. int getfile(result)
  51. char *result;
  52. {
  53.     SFReply reply;
  54.     SFTypeList    myTypes;  /* see StdFilePkg.h */
  55.     Point SFwhere;
  56.     
  57.     SFwhere.h = 100;  /* coordinates for dialog box */
  58.     SFwhere.v = 50;
  59.     
  60. /*    if(thePort == NULL){    
  61.         Init_stdio();
  62.         InitGraf(&thePort);
  63.         InitFonts();
  64.         InitWindows(); 
  65.     }*/
  66.     myTypes[0]='TEXT';
  67.     SFGetFile( SFwhere, "\p", 0L, 1, myTypes, 0L, &reply );
  68.  
  69.     if (reply.good) {
  70.         char buff[255],*p,*pathname();
  71.        register int i;
  72.  
  73.       p = PtoCstr((char *) reply.fName);
  74.       for(i = 0; buff[i] = p[i]; ++i)    /* copy name into a buffer capable of */
  75.           ;                                        /* holding the full path */
  76.         if(!pathname(buff,reply.vRefNum))    /* return -1 for path > 255 */
  77.             return -1;
  78.         for(i = 0; result[i] = buff[i]; ++i)
  79.             ;
  80.         return i;    /* return number of chars in pathname */
  81.     }
  82.     else
  83.         return 0; /* return 0 if bad reply */
  84. }
  85.  
  86. int putfile(result,prompt,defaultname)
  87. char *result,*prompt,*defaultname;
  88. {
  89.     SFReply reply; /* Standard File Reply see StdFilePkg.h*/
  90.     int wdRefNum; /* working directory reference Number */
  91.     SFTypeList    myTypes;
  92.     Point SFwhere;
  93.  
  94.     SFwhere.h = 100;  /* coordinates for dialog box */
  95.     SFwhere.v = 50;
  96.     
  97.     /*if(thePort == NULL){    Initializations 
  98.         InitGraf(&thePort);
  99.         InitFonts();
  100.         InitWindows();
  101.     }*/
  102.  
  103.     /*myTypes[0]='TEDC';*/
  104.     SFPutFile( SFwhere, CtoPstr(prompt), CtoPstr(defaultname),0L, &reply );
  105.     if (reply.good) {
  106.         char buff[255],*p,*pathname();
  107.        register int i;
  108.  
  109.       p = PtoCstr((char *) reply.fName);
  110.       for(i = 0; buff[i] = p[i]; ++i)    /* copy name into a buffer capable of */
  111.           ;                                        /* holding the full path */
  112.         if(!pathname(buff,reply.vRefNum))    /* return -1 for path > 255 */
  113.             return -1;
  114.         for(i = 0; result[i] = buff[i]; ++i)
  115.             ;
  116.         return i;    /* return number of chars in pathname */
  117.     }
  118.     else
  119.         return 0; /* return 0 if bad reply */
  120. }
  121.  
  122. /* modified from Mike Schuster, "Programming for HFS Compatibility ", the
  123. Complete MacTutor, Vol. 2, pg. 87. (Mac C) to Light Speed C, Jeff Meredith 
  124. 12/88 
  125.  
  126. Changes. 
  127. 1. removed requirement to pass size of wdrefnum(sizeof(wdrefnum)) as the
  128. third parameter.
  129. 2. removed need for strntac() which is not in LSC libraries.
  130. 3. minor typecasting changes to match LSC prototypes.
  131. 4. FSFCBLen defined in HFS.h, no need to define here */
  132.  
  133. #define mfsSigWord 0xd2d7
  134. #define hfsSigWord 0x4244
  135. #define rootDirID 2
  136. /*#define ioDirFlg 0x10*/
  137.  
  138. char *pathname(pname,wdrefnum)
  139. char *pname;
  140. int wdrefnum;
  141. {
  142.     HVolumeParam vp;
  143.     WDPBRec wp;
  144.     DirInfo dp;
  145.     Str255 dname;
  146.     char *strtac();
  147.     int length;
  148.  
  149.     /* initialize params */
  150.  
  151.     vp.ioNamePtr=(StringPtr)&dname;
  152.     vp.ioVRefNum=wdrefnum;
  153.     vp.ioVolIndex=0;
  154.     wp.ioNamePtr=0L;
  155.     wp.ioVRefNum=wdrefnum;
  156.     wp.ioWDIndex=0;
  157.     wp.ioWDProcID=0L;
  158.     dp.ioNamePtr=(StringPtr)&dname;
  159.     dp.ioFDirIndex=-1;
  160.  
  161.     /* get volume information */
  162.     if(!PBHGetVInfo(&vp,0))
  163.          /* if MFS or HFS root, return volume name */
  164.          if(FSFCBLen==-1||vp.ioVSigWord==mfsSigWord||vp.ioVRefNum==wdrefnum)
  165.              pname = PtoCstr((Ptr)vp.ioNamePtr);
  166.  
  167.      /*get working directory information */ 
  168.      else if (!PBGetWDInfo(&wp,0)){
  169.          /* traverse path from working directory to root */
  170.          dp.ioVRefNum=wp.ioWDVRefNum;
  171.          dp.ioDrParID=wp.ioWDDirID;
  172.  
  173.          for(length = 0; pname[length]; ++length)    /* compute length of pname */
  174.              ;
  175.  
  176.          do{
  177.              char *folder;
  178.              int i;
  179.              
  180.             /* get next node information */
  181.             dp.ioDrDirID=dp.ioDrParID;
  182.             if(PBGetCatInfo(&dp,0))
  183.                 break;
  184.         
  185.             /*concatenate node name to result*/
  186.             folder = PtoCstr((char *) dp.ioNamePtr);
  187.              for(i = 0; folder[i]; ++i)    /* compute length of folder */
  188.                  ;
  189.              if((length += i) > 255)
  190.                 return NULL;
  191.             pname = strtac(":",pname);
  192.             pname = strtac(folder,pname);
  193.         
  194.             }while(dp.ioDrDirID!=rootDirID);
  195.     }
  196.     return pname;
  197. }
  198.  
  199. char *strtac(s1,s2)
  200. /*
  201.     concatenate s1 and s2 and put in s2
  202. */
  203. register char *s1,*s2;
  204. {
  205.     register int i;
  206.     char buff[256];
  207.     register char *p;
  208.     
  209.     p = buff;
  210.  
  211.     for(i = 0; p[i] = s2[i]; ++i)
  212.         ;    
  213.     
  214.     for(i = 0; s2[i] = s1[i]; ++i)
  215.         ;
  216.  
  217.     for(; s2[i] = *p++; ++i)
  218.         ;
  219.  
  220.     return s2;
  221. }  
  222.  
  223.